package com.dingreading.cloud.mall.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.dto.LoginDataDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.mall.service.GoodsCategoryService;
import com.dingreading.cloud.mall.service.GoodsService;
import com.dingreading.cloud.mall.util.LoginInfoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/front/goods")
@Api(tags = "商城-商品管理")
public class FrontGoodsControllerV2 {

    @Resource
    protected GoodsService goodsService;
    @Resource
    protected GoodsCategoryService goodsCategoryService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "商品名") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "商品种类id") @RequestParam(value = "categoryId") Long categoryId
    ) {
        return goodsService.pageListV2(pageUtil, storeUid, staffUid, name, categoryId);
    }


    @ApiOperation(value = "根据加盟商合作类型获取商品种类")
    @PostMapping(value = "/getCategory", produces = "application/json; charset=UTF-8")
    public R<Object> getCategory(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName
    ) {
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();
        Integer operationType = loginDto.getCurrentOperationType();
        List<Integer> cooperationTypeList = loginDto.getCurrentCooperationTypes();

        return goodsCategoryService.getCategoryByGoodsPower(storeUid, operationType, cooperationTypeList);
    }


    @ApiOperation(value = "获取商品详情")
    @PostMapping(value = "/get", produces = "application/json; charset=UTF-8")
    public R<Object> get(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "商品id") @RequestParam(value = "id") Long id
    ) {
        return goodsService.get(storeUid, staffUid, id);
    }

}
